package ar.Location.Helper;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import ar.activity.R;

public class BitmapProvider {

	public static Resources res = null;
	public static Bitmap bmpNoImage = null;
	public static Bitmap bmpIconConference = null;
	public static Bitmap bmpIconEvent = null;
	public static Bitmap bmpIconCafe = null;
	public static Bitmap bmpIconFood = null;
	public static Bitmap bmpIconPark = null;
	public static Bitmap bmpIconBank = null;;
	public static Bitmap bmpIconUniversity = null;;
	public static Bitmap bmpIconHospital = null;;
	public static Bitmap bmpIconPhoto = null;
	public static Bitmap bmpIconFoursquare = null;
	public static Bitmap bmpIconWikipedia = null;
	public static Bitmap bmpIconWikipediaSphere;

	public static Drawable normalMarker = null;
	public static Drawable directionMarker = null;
	public static Drawable trackingMe = null;
	public static Drawable trackingFriend = null;

	public static Drawable drawableMarkerGooglePlace = null;
	public static Drawable drawableMarkerWikipedia = null;
	public static Drawable drawableMarkerFlickr = null;
	public static Drawable drawableMarkerFoursquare = null;
	public static Drawable drawableMarkerEventful = null;
	public static Drawable drawableMarkerCafe = null;
	public static Drawable drawableMarkerFood = null;
	public static Drawable drawableMarkerPark = null;
	public static Drawable drawableMarkerBank = null;
	public static Drawable drawableMarkerUniversity = null;
	public static Drawable drawableMarkerHospital = null;

	public static boolean isInit = false;

	public static void initBitmapProvider(Context context) {
		if (isInit == false) {
			res = context.getResources();
			bmpNoImage = BitmapFactory.decodeResource(res,
					R.drawable.icon_no_image);
			bmpIconConference = BitmapFactory.decodeResource(res,
					R.drawable.icon_conference);
			bmpIconEvent = BitmapFactory.decodeResource(res,
					R.drawable.icon_event);
			bmpIconPhoto = BitmapFactory.decodeResource(res,
					R.drawable.icon_photo);
			bmpIconWikipedia = BitmapFactory.decodeResource(res,
					R.drawable.icon_wikipedia);
			bmpIconWikipediaSphere = BitmapFactory.decodeResource(res,
					R.drawable.icon_wikipedia_sphere);
			bmpIconFoursquare = BitmapFactory.decodeResource(res,
					R.drawable.icon_foursquare);
			bmpIconCafe = BitmapFactory.decodeResource(res,
					R.drawable.icon_coffee);
			bmpIconFood = BitmapFactory.decodeResource(res,
					R.drawable.icon_food);
			bmpIconPark = BitmapFactory.decodeResource(res,
					R.drawable.icon_park);
			bmpIconBank = BitmapFactory.decodeResource(res,
					R.drawable.icon_bank);
			bmpIconUniversity = BitmapFactory.decodeResource(res,
					R.drawable.icon_university);
			bmpIconHospital = BitmapFactory.decodeResource(res,
					R.drawable.icon_hospital);

			normalMarker = res.getDrawable(R.drawable.icon_map_marker);
			directionMarker = res.getDrawable(R.drawable.icon_map_marker_2);
			trackingMe = res.getDrawable(R.drawable.tracking_my_location);
			trackingFriend = res
					.getDrawable(R.drawable.tracking_friends_location);

			drawableMarkerCafe = res.getDrawable(R.drawable.icon_coffee_small);
			drawableMarkerEventful = res
					.getDrawable(R.drawable.icon_eventful_small);
			drawableMarkerFlickr = res
					.getDrawable(R.drawable.icon_flickr_small);
			drawableMarkerFood = res.getDrawable(R.drawable.icon_food_small);
			drawableMarkerPark = res.getDrawable(R.drawable.icon_park_small);
			drawableMarkerBank = res.getDrawable(R.drawable.icon_bank_small);
			drawableMarkerUniversity = res
					.getDrawable(R.drawable.icon_university_small);
			drawableMarkerHospital = res
					.getDrawable(R.drawable.icon_hospital_small);
			drawableMarkerFoursquare = res
					.getDrawable(R.drawable.icon_foursquare_small);
			drawableMarkerGooglePlace = res
					.getDrawable(R.drawable.icon_google_place_small);
			drawableMarkerWikipedia = res
					.getDrawable(R.drawable.icon_wikipedia_small);

			setBoundMarker(normalMarker);
			setBoundMarker(directionMarker);
			setBoundMarker(trackingMe);
			setBoundMarker(trackingFriend);

			setBoundMarker(drawableMarkerCafe);
			setBoundMarker(drawableMarkerEventful);
			setBoundMarker(drawableMarkerFlickr);
			setBoundMarker(drawableMarkerFood);
			setBoundMarker(drawableMarkerPark);
			setBoundMarker(drawableMarkerBank);
			setBoundMarker(drawableMarkerUniversity);
			setBoundMarker(drawableMarkerHospital);
			setBoundMarker(drawableMarkerFoursquare);
			setBoundMarker(drawableMarkerGooglePlace);
			setBoundMarker(drawableMarkerWikipedia);

			isInit = true;
		}
	}

	public static void setBoundMarker(Drawable markerIcon) {
		markerIcon.setBounds((int) (-markerIcon.getIntrinsicWidth() / 2),
				-markerIcon.getIntrinsicHeight(),
				(int) (markerIcon.getIntrinsicWidth() / 2), 0);
	}

	public static Bitmap getBitmapFromURL(String src) {
		try {
			URL url = new URL(src);
			InputStream is = url.openConnection().getInputStream();
			Bitmap myBmp = BitmapFactory.decodeStream(is);
			return myBmp;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
